// pages/shopcart/shopcart.js
import {reqGetCartListData,reqHandleChangeChecked,reqHandleChangeAllChecked,reqDeleteGoods, reqAddToCart} from '../../utils/api'
import {showToast} from '../../utils/tips'
Page({
  //获取购物车列表数据
  async getCartList(){
    const result = await reqGetCartListData()
    // console.log(result)
    if(result.code === 200){
      this.setData({
        cartList:result.data
      })
    }else{
      showToast(result.data)
    }
    const {cartList} =  this.data
    const flag =  cartList.every((item) => item.isChecked === 1)&&cartList.length
    // console.log(flag)
    const isAllChecked = flag - 0
    // console.log(isAllChecked)
    this.setData({
      isAllChecked,
    })
     //渲染商品总数量和价格
     this.compoutGoodsPriceAndTotal()
  },

  //购物车单选
  async handleChangeChecked(e){
    // console.log(e)
    const {cartList} = this.data
    let goodsId = e.target.dataset.goodsid
    const cartItem = cartList.find(item => item.goodsId === goodsId)
    const isChecked = 1 - cartItem.isChecked
    cartItem.isChecked = isChecked
    //选中的item长度 === list长度 则全选按钮选中
    const checkedItemsLength = cartList.filter(item => item.isChecked === 1).length
    const result = await reqHandleChangeChecked({goodsId,isChecked})
    if(result.code === 200){
      this.setData({
        cartList,
        isAllChecked:cartList.length === checkedItemsLength
      })
    }
     //渲染商品总数量和价格
     this.compoutGoodsPriceAndTotal()
  },

  //购物车全选
  async handleChangeAllChecked(e){
    // console.log(e)
    const {cartList} = this.data
    const isAllChecked = 1 - e.target.dataset.isallchecked
    //遍历循环，全选/全不选
    cartList.forEach(item => {
      item.isChecked = isAllChecked
    })
    const result = await reqHandleChangeAllChecked(isAllChecked)
    if(result.code === 200){
      //重新渲染数据
      this.setData({
        cartList,
        isAllChecked,
      })
    }
     //渲染商品总数量和价格
     this.compoutGoodsPriceAndTotal()
  },

  //计算商品总金额/数量
  compoutGoodsPriceAndTotal(){
    const {cartList} = this.data
    const totalPrice = cartList.reduce((preve,item) => preve + item.price * item.count * item.isChecked, 0)
    const goodsCount = cartList.filter(item => item.isChecked ===1).length
    this.setData({
      totalPrice,
      goodsCount
    })
  },

  //删除购物车商品
  handleDelete(e){
    const {goodsid,name} = e.target.dataset
    wx.showModal({
      title: '确认删除',
      content: name,
      confirmColor:'red',
      success:async(res) =>{
        if (res.confirm) {
         const result = await reqDeleteGoods(goodsid)
         if(result.code === 200){
          this.getCartList()
         }
        } else if (res.cancel) {
          
        }
      }
    })
  },

  //去结算
  handleToOrder(){
    wx.navigateTo({
      url: '/pages/order/detail/detail',
    })
  },

  //购物车更改商品数量
  async handleChangeCount(e){
    // console.log(e)
    const {goodsid,oldcount} = e.target.dataset
    const newCount = e.detail - oldcount
    const result = await reqAddToCart({
      goodsId:goodsid,
      count:newCount,
      blessing:''
    })
    if(result.code === 200){
      this.getCartList()
    }else{
      showToast(result.data,'error')
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    cartList:[],//购物车列表数据
    isAllChecked:false, //全部选中
    cartItemChecked:0,//选中状态的长度
    totalPrice:0,//商品总金额
    goodsCount:0,//商品总数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCartList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})